package com.interview.service.impl;

import com.interview.dao.ArticleMapper;
import com.interview.dao.CatalogueMapper;
import com.interview.dao.TitleMapper;
import com.interview.pojo.Article;
import com.interview.pojo.Catalogue;
import com.interview.pojo.Title;
import com.interview.service.TitleService;
import com.interview.utils.RedisUtis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service("TitleServiceImpl")
public class TitleServiceImpl implements TitleService {


    @Autowired
    private CatalogueMapper catalogueMapper;

    @Autowired
    private TitleMapper titleMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public void add(Title title) {

        try {
            titleMapper.insertSelective(title);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void del(Title title) {
        //删除主题
        Title title1 = queryID(title);
        System.out.println(title1);
        titleMapper.deleteByPrimaryKey(title.getId());

        //删除标题
        Catalogue record=new Catalogue();
        record.setTitle_id(title.getId());
        catalogueMapper.deleteByExample(createExampleDelete( record));


        //删除标题内所有文章
        List<Catalogue> catalogues = catalogueMapper.selectByExample(createExampleSelect(record));
        for (Catalogue catalogue : catalogues) {
            articleMapper.deleteByExample(createExampleDelete_Article(catalogue));
        }

        //删除缓存
        RedisUtis.delKeyRedis("interview-queryAll-Article");
        queryAll(); //更新缓存


    }


    public Example createExampleDelete(Catalogue record) {
        Example example = new Example(Catalogue.class);
        Example.Criteria criteria = example.createCriteria();
        if(record!=null){
            // 如果name不为空 模糊匹配查询name字段
            if(!StringUtils.isEmpty(record.getTitle_id())){
                criteria.andEqualTo("title_id",record.getTitle_id());
            }

            //去重复
            example.setDistinct(true);
        }

        return example;
    }
    public Example createExampleSelect(Catalogue record) {
        Example example = new Example(Catalogue.class);
        Example.Criteria criteria = example.createCriteria();
        if(record!=null){
            // 如果name不为空 模糊匹配查询name字段
            if(!StringUtils.isEmpty(record.getTitle_id())){
                criteria.andEqualTo("title_id",record.getTitle_id());
            }

            //去重复
            example.setDistinct(true);
        }

        return example;
    }


    public Example createExampleDelete_Article(Catalogue record) {
        Example example = new Example(Article.class);
        Example.Criteria criteria = example.createCriteria();
        if(record!=null){
            // 如果name不为空 模糊匹配查询name字段
            if(!StringUtils.isEmpty(record.getId())){
                criteria.andEqualTo("catalogue_id",record.getId());
            }

            //去重复
            example.setDistinct(true);
        }

        return example;
    }


    @Override
    public List<Title> query(Title title) {
        List<Title> select = titleMapper.select(title);
        return  select;
    }

    @Override
    public int count() {
       return  titleMapper.count();
    }

    @Override
    public void remove() {

    }

    @Override
    public void alter(Title title) {
        titleMapper.updateByPrimaryKeySelective(title);
    }

    @Override
    public  List<Title> queryAll() {
            List<Title> titles = titleMapper.selectAll();

            return  titles;
    }

    @Override
    public Title query_one(Title title) {
        return titleMapper.selectByPrimaryKey(title);
    }

    @Override
    public Title queryID(Title catalogue) {
        return titleMapper.selectByPrimaryKey(catalogue.getId());
    }


}
